package vistas;
import clases.Seccion;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FrmProfesor extends javax.swing.JFrame {
	private JPanel jPanel2;
	private JButton jButton1;
	private JLabel jLabel4;
	private JTextField titulo;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JTextField cedula;
	private JTextField apellido;
	private AbstractAction abstractAction3;
	private AbstractAction abstractAction2;
	private AbstractAction abstractAction1;
	private JTextField nombre;
	private JLabel jLabel1;
	private JPanel jPanel1;
	private JButton jButton3;
	private JButton jButton2;
	private Seccion seccion = null;
	private Vector<JTextField> textos;
	

	public Seccion getSeccion() {
		return seccion;
	}

	public void setSeccion(Seccion seccion) {
		this.seccion = seccion;
	}

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				FrmProfesor inst = new FrmProfesor();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public FrmProfesor() {
		super();
		initGUI();
		textos = new Vector<JTextField>();
		textos.add(apellido);
		textos.add(nombre);
		textos.add(cedula);
		textos.add(titulo);
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Registro de Profesor");
			getContentPane().setLayout(null);
			{
				jPanel2 = new JPanel();
				getContentPane().add(jPanel2);
				jPanel2.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
				AnchorLayout jPanel1Layout = new AnchorLayout();
				jPanel2.setLayout(jPanel1Layout);
				jPanel2.setBounds(25, 221, 292, 60);
				jPanel2.setLayout(null);
				{
					jButton2 = new JButton();
					jPanel2.add(jButton2);
					jButton2.setText("jButton2");
					jButton2.setAction(getAbstractAction2());
					jButton2.setBounds(1, 22, 105, 22);
					jButton2.setAction(getAbstractAction1());
				}
				{
					jButton3 = new JButton();
					jPanel2.add(jButton3);
					jButton3.setText("jButton3");
					jButton3.setAction(getAbstractAction3());
					jButton3.setBounds(111, 22, 102, 22);
					jButton3.setAction(getAbstractAction2());
				}
				{
					jButton1 = new JButton();
					jPanel2.add(jButton1);
					jButton1.setText("jButton1");
					jButton1.setAction(getAbstractAction1());
					jButton1.setBounds(213, 22, 67, 22);
					jButton1.setAction(getAbstractAction3());
				}
			}
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1);
				AnchorLayout jPanel1Layout = new AnchorLayout();
				jPanel1.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
				jPanel1.setLayout(jPanel1Layout);
				jPanel1.setBounds(25, 40, 292, 151);
				{
					jLabel1 = new JLabel();
					jPanel1.add(jLabel1, new AnchorConstraint(76, 361, 162, 101, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jLabel1.setText("Cedula:");
					jLabel1.setPreferredSize(new java.awt.Dimension(76, 13));
				}
				{
					nombre = new JTextField();
					jPanel1.add(nombre, new AnchorConstraint(317, 888, 441, 556, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					nombre.setPreferredSize(new java.awt.Dimension(97,22));
				}
				{
					apellido = new JTextField();
					jPanel1.add(apellido, new AnchorConstraint(575, 888, 699, 556, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					apellido.setPreferredSize(new java.awt.Dimension(97,22));
				}
				{
					cedula = new JTextField();
					jPanel1.add(cedula, new AnchorConstraint(58, 888, 182, 556, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					cedula.setPreferredSize(new java.awt.Dimension(97,22));
				}
				{
					jLabel2 = new JLabel();
					jPanel1.add(jLabel2, new AnchorConstraint(334, 361, 418, 101, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jLabel2.setText("Nombre:");
					jLabel2.setPreferredSize(new java.awt.Dimension(76,15));
				}
				{
					jLabel3 = new JLabel();
					jPanel1.add(jLabel3, new AnchorConstraint(575, 361, 660, 104, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jLabel3.setText("Apellido:");
					jLabel3.setPreferredSize(new java.awt.Dimension(75,15));
				}
				{
					titulo = new JTextField();
					jPanel1.add(titulo, new AnchorConstraint(791, 888, 917, 556, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					titulo.setPreferredSize(new java.awt.Dimension(97, 19));
				}
				{
					jLabel4 = new JLabel();
					jPanel1.add(jLabel4, new AnchorConstraint(811, 559, 897, 104, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jLabel4.setText("Titulo Obtenido:");
					jLabel4.setPreferredSize(new java.awt.Dimension(133, 13));
				}
			}
			pack();
			this.setSize(357, 323);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private AbstractAction getAbstractAction1() {
		if(abstractAction1 == null) {
			abstractAction1 = new AbstractAction("Registrar", null) {
				public void actionPerformed(ActionEvent evt) {
					for (Iterator iterator = textos.iterator(); iterator.hasNext();) {
						JTextField texto = (JTextField) iterator.next();
						if (texto.getText().isEmpty()){
							JOptionPane.showMessageDialog(new JFrame(),"Existen camoos vacios","Registro de Estudiantes",JOptionPane.WARNING_MESSAGE);
							return;			
						}
					}
						try {
							int ced = Integer.parseInt(cedula.getText());
							if(ced<=0){
								JOptionPane.showMessageDialog(new JFrame(),"Ingrese  una cedula valida","Registro de Profesor",JOptionPane.WARNING_MESSAGE);
								return;
							}
						}catch (NumberFormatException ex) {
							JOptionPane.showMessageDialog(new JFrame(),"Ingrese  una cedula valida","Registro de Profesor",JOptionPane.WARNING_MESSAGE);
							return;
						}
						if(seccion.getNum_Seccion()==-1){
							JOptionPane.showMessageDialog(new JFrame(),"Debe crear la seccion","Registro de Profesor",JOptionPane.WARNING_MESSAGE);
							return;
						}
						seccion.prof_Asig(Integer.parseInt(cedula.getText()), nombre.getText(), apellido.getText(),titulo.getText());
						JOptionPane.showMessageDialog(new JFrame(),"Registro Exitoso","Registro de Profesor",JOptionPane.INFORMATION_MESSAGE);
						cancelar();
					
					}
			};
		}
		return abstractAction1;
	}
	
	
	 public void cancelar(){
		 for (Iterator iterator = textos.iterator(); iterator.hasNext();) {
				JTextField texto = (JTextField) iterator.next();
				texto.setText("");
			}
	 }
	private AbstractAction getAbstractAction2() {
		if(abstractAction2 == null) {
			abstractAction2 = new AbstractAction("Cancelar", null) {
				public void actionPerformed(ActionEvent evt) {
					cancelar();
				}
			};
		}
		return abstractAction2;
	}
	
	private AbstractAction getAbstractAction3() {
		if(abstractAction3 == null) {
			abstractAction3 = new AbstractAction("Salir", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return abstractAction3;
	}

}
